/*
 * QueryController.java
 *
 * Created on 2007��12��4��, ����5:09
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.microweb.toastmaster.webapp.action;

import com.microweb.toastmaster.Constants;
import com.microweb.toastmaster.model.Query;
import com.microweb.toastmaster.model.Role;
import com.microweb.toastmaster.model.TMRole;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.microweb.toastmaster.service.SpeechManager;
import com.microweb.toastmaster.service.UserManager;
import com.microweb.toastmaster.service.TMRoleManager;
import com.microweb.toastmaster.service.AgendaManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

public class QueryController extends BaseFormController {
    private SpeechManager speechManager = null;
    private UserManager userManager = null;
    private TMRoleManager TMRoleManager = null;
    private AgendaManager agendaManager = null;

    public void setSpeechManager(SpeechManager speechManager) {
        this.speechManager = speechManager;
    }
    
    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
    
    public void setTMRoleManager(TMRoleManager TMRoleManager) {
        this.TMRoleManager = TMRoleManager;
    }
    
    public void setAgendaManager(AgendaManager agendaManager) {
        this.agendaManager = agendaManager;
    }
    
    public QueryController() {
        setCommandName("query");
        setCommandClass(Query.class);
    }

    protected Object formBackingObject(HttpServletRequest request)
    throws Exception {
        Query query = new Query();
        return query;
    }

    public ModelAndView onSubmit(HttpServletRequest request,
                                 HttpServletResponse response, Object command,
                                 BindException errors)
    throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("entering 'onSubmit' method...");
        }

	Query query = (Query)command;
	log.info(query);
	List agendas = agendaManager.query(query);
	log.info("agendas.size = " + agendas.size());
	
	return new ModelAndView("matchedAgendas", Constants.AGENDA_LIST, agendas);
    }
    
       protected Map referenceData(HttpServletRequest request) throws Exception {  
	 HashMap referenceData = new HashMap();  
	 List users = userManager.getUsers(null);
	 referenceData.put("availableUsers",users);
	 
	 List tmroles = TMRoleManager.getTMRoles(null);
	 removeRole(tmroles, Constants.ROLENAME_SPEAKER1);
	 removeRole(tmroles, Constants.ROLENAME_SPEAKER2);
	 removeRole(tmroles, Constants.ROLENAME_SPEAKER3);
	 removeRole(tmroles, Constants.ROLENAME_EVALUATOR1);
	 removeRole(tmroles, Constants.ROLENAME_EVALUATOR2);
	 removeRole(tmroles, Constants.ROLENAME_EVALUATOR3);
	 
	 addRole(tmroles,Constants.ROLENAME_SPEAKER);
	 addRole(tmroles,Constants.ROLENAME_EVALUATOR);
	 
	 referenceData.put("availableTMRoles",tmroles);
         return referenceData;   
     }
       
     private void removeRole(List roles, String roleName) {
	 for(int i=0; i<roles.size(); i++) {
	     TMRole role = (TMRole)roles.get(i);
	     if(role.getName().equalsIgnoreCase(roleName))
		 roles.remove(i);
	 }
     }
     
     private void addRole(List roles, String roleName) {
	 Role role = new Role();
	 role.setName(roleName);
	 roles.add(role);
     }
       
}

